﻿using System;
using System.Net;
using System.Text;
using PushEmail.Common;
using PushEmail.Common.Logging;

namespace MyMailWp8.Protocol
{
    internal class CMSmtpLogin : CMPopBase
    {
        //private String userName;
        //private String passWord;

        //private String version;
        //private String phoneType;

        private int _loginState = 0;//
        private String _stringRawDatabuf;

        public CMSmtpLogin(AsyncSocketClient sockClient)
            : base(sockClient)
        {
            _stringRawDatabuf = null;
        }

        public bool userLogin()
        {
            return true;
        }

        public bool parseLoginResult(String inData)
        {
            return true;
        }

        public bool userLogout()
        {

            return true;
        }

        override public int ParseData(String inString)
        {
            if (inString == null)
                return 0;

            _stringRawDatabuf += inString;

            switch (_loginState)
            {

                //220 China Mobile push proxy v1.0 ready
                case 0:
                    //waiting for OK message
                    if (inString.StartsWith("220") == true)
                    {
                        _loginState = 1;
                        _stringRawDatabuf = null;
                        SendData("EHLO pe-wp7\r\n");
                    }
                    else
                    {
                        PostServerRetError((int)SockMessageType.LoginFail);
                        DefaultLogger.Log.LogDebug(inString);
                        DefaultLogger.Log.LogDebug("not receive 220 China Mobile push proxy v1.0 ready");
                    }
                    break;

                //waiting for:
                //S: 250-SMTP.example.com offers extensions: 
                //S: 250-PIPELINING 
                //S: 250-AUTH LOGIN CRAM-MD5 PLAIN 
                //S: 250 8BITMIME 
                case 1:
                    //waiting for OK message
                    if (_stringRawDatabuf.ToUpper().StartsWith("250") == true)
                    {
                        if (_stringRawDatabuf.EndsWith("\r\n") == true)
                        {
                            //got server response,change state and send AUTH LOGIN request                            
                            _loginState = 2;
                            _stringRawDatabuf = null;
                            SendData("AUTH LOGIN\r\n");
                        }
                        else
                        {
                            //receive the more remaining data
                            ReceiveData();
                        }
                    }
                    else
                    {
                        PostServerRetError((int)SockMessageType.LoginFail);
                        DefaultLogger.Log.LogDebug(_stringRawDatabuf);
                        DefaultLogger.Log.LogDebug("not receive welcome message");
                        //
                    }
                    break;

                //waiting for AUTH LOGIN response
                //334 VXNlcm5hbWU6 
                case 2:
                    if (_stringRawDatabuf.StartsWith("334") == true)
                    {
                        _loginState = 3;
                        _stringRawDatabuf = null;

                        Account account = GetAccountInfo();
                        if (account != null)
                        {
                            SendData(Convert.ToBase64String(Encoding.UTF8.GetBytes(account.emailAddress)) + "\r\n");
                            //    SendData("USER 13910295587@139.com 2.0.0 GC001 5.5B20111121\r\n");
                        }
                        else
                        {
                            DefaultLogger.Log.LogDebug("can't get account info");
                            ////
                            string useName = Convert.ToBase64String(Encoding.UTF8.GetBytes("13910295587@139.com"));
                            SendData(useName + "\r\n");
                        }
                    }
                    else
                    {
                        DefaultLogger.Log.LogDebug(_stringRawDatabuf);
                        DefaultLogger.Log.LogDebug("waiting for AUTH LOGIN response: not receive 334 VXNlcm5hbWU6 ");
                        //
                    }
                    break;

                //waiting for: 334 UGFzc3dvcmQ6
                case 3:
                    if (_stringRawDatabuf.StartsWith("334") == true)
                    {
                        _loginState = 4;
                        _stringRawDatabuf = null;

                        Account account = GetAccountInfo();
                        if (account != null)
                        {
                            SendData(Convert.ToBase64String(Encoding.UTF8.GetBytes(account.password)) + "\r\n");
                        }
                        else
                        {
                            DefaultLogger.Log.LogDebug("can't get account info");
                            ////
                            string passWord = Convert.ToBase64String(Encoding.UTF8.GetBytes("****"));
                            SendData(passWord + "\r\n");
                        }
                    }
                    else
                    {
                        PostServerRetError((int)SockMessageType.LoginFail);
                        DefaultLogger.Log.LogDebug(_stringRawDatabuf);
                        DefaultLogger.Log.LogDebug("\r\nnot receive +OK maildrop locked and ready\r\n");
                    }
                    break;

                //waiting for: 235 Authentication successful.
                case 4:
                    if (_stringRawDatabuf.StartsWith("235") == true)
                    {
                        _loginState = 10;
                        int nState = (int)(CMSmtpState.CMSmtp_Send);
                        ChangeCMPopSmtpState(nState);
                    }
                    else
                    {
                        PostServerRetError((int)SockMessageType.LoginFail);
                        DefaultLogger.Log.LogDebug(_stringRawDatabuf);
                        DefaultLogger.Log.LogDebug("\r\nnot receive 235 Authentication successful\r\n");
                    }
                    break;
                default:
                    break;
            }

            return 0;
        }
    }
}
